<template>
  <div class="add">
    <breadcrumb :first="'商品管理'"></breadcrumb>
    <el-card>
      <!-- 提醒信息 -->
      <el-alert
        :closable="false"
        center
        title="添加商品信息"
        type="info"
        show-icon
      >
      </el-alert>
      <!-- 进度条 -->
      <el-steps
        :space="200"
        align-center
        :active="activename - 0"
        finish-status="success"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- 数据表单 -->
      <el-form
        :model="addform"
        :rules="rules"
        ref="addform"
        label-width="140px"
        class="demo-ruleForm"
      >
        <el-tabs
          v-model="activename"
          tab-position="left"
          style="height: aotu"
          :before-leave="tabLeave"
          @tab-click="changetab"
        >
          <!-- 基本信息 -->
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addform.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="addform.goods_price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="addform.goods_weight" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="addform.goods_number" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="goods_cat">
              <el-cascader
                clearable
                v-model="addform.goods_cat"
                :options="catelist"
                :props="cascaderProps"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <!-- 商品参数 -->
          <el-tab-pane label="商品参数" name="1">
            <el-form-item
              v-for="val in paramslist"
              :key="val.attr_id"
              :label="val.attr_name"
            >
              <el-tag v-for="(item, i) in val.attr_vals" :key="i">{{
                item
              }}</el-tag>
            </el-form-item>
          </el-tab-pane>
          <!-- 商品属性 -->
          <el-tab-pane label="商品属性" name="2">
            <el-form-item
              v-for="val in onlylist"
              :key="val.attr_id"
              :label="val.attr_name"
            >
              <el-input v-model="val.attr_vals" width="180"></el-input>
            </el-form-item>
          </el-tab-pane>
          <!-- 商品图片 -->
          <el-tab-pane label="商品图片" name="3">
            <el-upload
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-tab-pane>
          <!-- 商品内容 -->
          <el-tab-pane label="商品内容" name="4">
            <quill-editor v-model="addform.goods_introduce"></quill-editor>
            <el-button class="addbtn" @click="add" type="primary">点击添加</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import breadcrumb from "@/components/breadcrumb.vue";
import { categorieslist, getparameterlist ,addgoods} from "@/api/index";
export default {
  data() {
    return {
      activename: "0",
      addform: {
        goods_name: "",
        goods_price: 0,
        goods_weight: 0,
        goods_number: 0,
        goods_cat: [],
        goods_introduce: ''
      },
      catelist: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_cat: [
          { required: true, message: "请选择商品分类", trigger: "blur" },
        ],
      },
      paramslist: [],
      onlylist: [],
      
    };
  },
  components: {
    breadcrumb,
  },
  created() {
    this.getcatelist();
  },
  methods: {
    getcatelist() {
      categorieslist().then((res) => {
        this.catelist = res.data.data;
      });
    },
    //级联选择器触发函数
    handleChange() {
      if (this.addform.goods_cat.length < 3) {
        this.addform.goods_cat = [];
        return;
      }
    },
    //标签页钩子函数
    tabLeave(activeName, oldActiveName) {
      if (oldActiveName == 0 && this.addform.goods_cat.length < 3) {
        this.$message.error("请完善基础信息");
        return false;
      }
    },
    //标签页触发函数
    changetab() {
      if (this.activename == 1) {
        getparameterlist(this.addform.goods_cat[2], "many").then((res) => {
          res.data.data.forEach((item) => {
            item.attr_vals =
              item.attr_vals.length == 0 ? [] : item.attr_vals.split(",");
          });
          this.paramslist = res.data.data;
        });
      }
      if (this.activename == 2) {
        getparameterlist(this.addform.goods_cat[2], "only").then((res) => {
          console.log(res.data);
          this.onlylist = res.data.data;
        });
      }
    },
    //上传图片触发函数
    handlePreview(){},
    handleRemove(){},
    //添加商品
    add(){
      this.addform.goods_cat = this.addform.goods_cat.join(',')
      addgoods(this.addform).then( res=>{
        if(res.data.meta.status !== 201){
          this.$message.error(res.data.meta.msg)
        } else{
          this.$router.push('/goods')
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        }
      })
    }
  },
};
</script>

<style lang="less" scoped>
.add {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 10px;
  .el-steps {
    margin: 10px 0;
  }
  /deep/.el-step__title {
    font-size: 14px;
  }
  .el-tag {
    margin-right: 5px;
  }
  /deep/.ql-editor{
    min-height: 200px;
  }
  .addbtn{
    margin-top: 5px;
  }
}
</style>
